﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Shop.Core.Share;
using Shop.IServices.Admin;
using Shop.Model.Dto.Admin.JurUser;
using Shop.Model.View.Admin.JurUser;

namespace Shop.AdminApi.Controllers;

/// <summary>
/// 角色管理
/// </summary>
[ApiController]
[Route("admin/[controller]")]
public class JurUserController: ShopControllerBase
{
    private readonly IJurUserService _userService;
    private readonly IUserContext _userContext;
    
    /// <summary>
    /// 
    /// </summary>
    /// <param name="userService"></param>
    /// <param name="userContext"></param>
    public JurUserController(IJurUserService userService, IUserContext userContext)
    {
        _userService = userService;
        _userContext = userContext;
    }
    
    /// <summary>
    /// 添加用户
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    [HttpPost("add")]
    [Authorize("Permission")]
    public async Task<AppResult> AddUser(UserAddDto dto)
    {
        try
        {
            return await _userService.AddUser(dto);
        }
        catch (Exception ex)
        {
            return Problem(ex.ToString());
        }
    }

    /// <summary>
    /// 用户列表
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    [HttpGet("list")]
    [Authorize("Permission")]
    public async Task<AppResult<PageModel<SysUserInforVo>>> GetSysUserList([FromQuery] SysUserPageSearchDto dto)
    {
        return await _userService.GetUserList(dto);
    }

    /// <summary>
    /// 修改用户信息
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    [HttpPost("update")]
    [Authorize("Permission")]
    public async Task<AppResult> Update(UserModifyDto dto)
    {
        try
        {
            return await _userService.Update(dto);
        }
        catch (Exception ex)
        {
            return Problem(ex.ToString());
        }
        
    }

    /// <summary>
    /// 修改密码
    /// </summary>
    /// <param name="input">密码</param>
    /// <returns></returns>
    [HttpPost("ChangeOwnPassWord")]
    public async Task<AppResult> ChangePassWord(SimpleDto<string> input)
    {
        return await _userService.ChangePassWord(_userContext.Id, input.Value);
    }

    /// <summary>
    /// 获取用户的菜单权限列表
    /// </summary>
    /// <returns></returns>
    [HttpGet("MenuList")]
    public async Task<AppResult<List<MenuListVo>>> GetUserMenu()
        => await _userService.GetUserMenu(_userContext.Id);
}